#pragma once
#include "stdafx.h"
#include "CheckFitness.h"
#include "GbcParser.h"
#include "CornerDetect.h"
#include <string>

bool SNU_CHECK_FITNESS_TEAR(char** ppImageArrays, CCornerInfo* pCornerInfo, int nBillType, int nBillDirection)
{
	int scale = 0xff;
	int holewidth[2] = {0, 0};
	bool holeheight = true;
	bool diagonal = true;
	int temp = 0;
	std::pair<int,int> point1, point2, point3;
	
	int Width = pCornerInfo->GetWidth();
	int Height = pCornerInfo->GetHeight();

	for (int i = 10; i < Width - 10; i += 5)
	{
		point1.first = i;
		point1.second = 5;
		point2.first = i;
		point2.second = 14;
		point3.first = i;
		point3.second = 5;

		if ((unsigned char) ppImageArrays[3][pCornerInfo->GetConverted(point1,3)] == scale)
		{
			while ((unsigned char) ppImageArrays[3][pCornerInfo->GetConverted(point1,3)] == scale)
			{
				if (holeheight && ((unsigned char) ppImageArrays[3][pCornerInfo->GetConverted(point2,3)] == scale))
					holeheight = false;
				
				point1.first++;
				point2.first++;
			}

			holewidth[0] = point1.first;
			point1.first = i;
			point2.first = i;

			while ((unsigned char) ppImageArrays[3][pCornerInfo->GetConverted(point1,3)] == scale)
			{
				if (holeheight && ((unsigned char) ppImageArrays[3][pCornerInfo->GetConverted(point2,3)] == scale))
					holeheight = false;

				point1.first--;
				point2.first--;
			}

			holewidth[1] = point1.first;

			if ((abs (holewidth[1] - holewidth[0]) > 5) && !holeheight)
				return false;

			diagonal = true;
			while(true)
			{
				diagonal = true;
				point3.second++;
				while ((unsigned char) ppImageArrays[3][pCornerInfo->GetConverted(point3,3)] == scale)
				{
					point3.second++;
					diagonal = false;
				}
				point3.second--;
				if(diagonal)
				{
					if ((((point3.first - i) * (point3.first - i)) + ((point3.second - 5) * (point3.second - 5))) > 255)
						return false;
					else
						break;
				}
				while ((unsigned char) ppImageArrays[3][pCornerInfo->GetConverted(point3,3) + 1] == scale)
				{
					point3.first++;
					diagonal = true;
				}
				if (!diagonal)
				{
					while ((unsigned char) ppImageArrays[3][pCornerInfo->GetConverted(point3,3) - 1] == scale)
					{
						point3.first--;
						diagonal = true;
					}
				}
			}
		}

		point1.first = i;
		point1.second = pCornerInfo->GetHeight() - 5;
		point2.first = i;
		point2.second = pCornerInfo->GetHeight() - 14;
		point3.first = i;
		point3.second = pCornerInfo->GetHeight() - 5;

		if ((unsigned char) ppImageArrays[3][pCornerInfo->GetConverted(point1,3)] == scale)
		{
			while ((unsigned char) ppImageArrays[3][pCornerInfo->GetConverted(point1,3)] == scale)
			{
				if (holeheight && ((unsigned char) ppImageArrays[3][pCornerInfo->GetConverted(point2,3)] == scale))
					holeheight = false;
				
				point1.first++;
				point2.first++;
			}

			holewidth[0] = point1.first;
			point1.first = i;
			point2.first = i;

			while ((unsigned char) ppImageArrays[3][pCornerInfo->GetConverted(point1,3)] == scale)
			{
				if (holeheight && ((unsigned char) ppImageArrays[3][pCornerInfo->GetConverted(point2,3)] == scale))
					holeheight = false;

				point1.first--;
				point2.first--;
			}

			holewidth[1] = point1.first;

			if ((abs (holewidth[1] - holewidth[0]) > 5) && !holeheight)
				return false;

			diagonal = true;
			while(true)
			{
				diagonal = true;
				point3.second--;
				while ((unsigned char) ppImageArrays[3][pCornerInfo->GetConverted(point3,3)] == scale)
				{
					point3.second--;
					diagonal = false;
				}
				point3.second++;
				if(diagonal)
				{
					if ((((point3.first - i) * (point3.first - i)) + ((point3.second - 5) * (point3.second - 5))) > 255)
						return false;
					else
						break;
				}
				while ((unsigned char) ppImageArrays[3][pCornerInfo->GetConverted(point3,3) + 1] == scale)
				{
					point3.first++;
					diagonal = true;
				}
				if (!diagonal)
				{
					while ((unsigned char) ppImageArrays[3][pCornerInfo->GetConverted(point3,3) - 1] == scale)
					{
						point3.first--;
						diagonal = true;
					}
				}
			}
		}
	}

	holeheight = true;

	for (int i = 10; i < Height - 10; i += 5)
	{
		point1.first = 5;
		point1.second = i;
		point2.first = 26;
		point2.second = i;
		point3.first = 5;
		point3.second = i;

		if ((unsigned char) ppImageArrays[3][pCornerInfo->GetConverted(point1,3)] == scale)
		{
			while ((unsigned char) ppImageArrays[3][pCornerInfo->GetConverted(point1,3)] == scale)
			{
				if (holeheight && ((unsigned char) ppImageArrays[3][pCornerInfo->GetConverted(point2,3)] == scale))
					holeheight = false;
				
				point1.second++;
				point2.second++;
			}

			holewidth[0] = point1.second;
			point1.second = i;
			point2.second = i;

			while ((unsigned char) ppImageArrays[3][pCornerInfo->GetConverted(point1,3)] == scale)
			{
				if (holeheight && ((unsigned char) ppImageArrays[3][pCornerInfo->GetConverted(point2,3)] == scale))
					holeheight = false;
				
				point1.second--;
				point2.second--;
			}

			holewidth[1] = point1.second;

			if ((abs (holewidth[1] - holewidth[0]) > 5) && !holeheight)
				return false;

			diagonal = true;
			while(true)
			{
				diagonal = true;
				point3.first++;
				while ((unsigned char) ppImageArrays[3][pCornerInfo->GetConverted(point3,3)] == scale)
				{
					point3.first++;
					diagonal = false;
				}
				point3.first--;
				if(diagonal)
				{
					if ((((point3.first - 5) * (point3.first - 5)) + ((point3.second - i) * (point3.second - i))) > 255)
						return false;
					else
						break;
				}

				point3.second++;
				while ((unsigned char) ppImageArrays[3][pCornerInfo->GetConverted(point3,3)] == scale)
				{
					point3.second++;
					diagonal = true;
				}
				point3.second--;
				if (!diagonal)
				{
					point3.second--;
					while ((unsigned char) ppImageArrays[3][pCornerInfo->GetConverted(point3,3)] == scale)
					{
						point3.second--;
						diagonal = true;
					}
					point3.second++;
				}
			}
		}

		point1.first = pCornerInfo->GetWidth() - 5;
		point1.second = i;
		point2.first = pCornerInfo->GetWidth() - 26;
		point2.second = i;
		point3.first = pCornerInfo->GetWidth() - 5;
		point3.second = i;

		if ((unsigned char) ppImageArrays[3][pCornerInfo->GetConverted(point1,3)] == scale)
		{
			while ((unsigned char) ppImageArrays[3][pCornerInfo->GetConverted(point1,3)] == scale)
			{
				if (holeheight && ((unsigned char) ppImageArrays[3][pCornerInfo->GetConverted(point2,3)] == scale))
					holeheight = false;
				
				point1.second++;
				point2.second++;
			}

			holewidth[0] = point1.second;
			point1.second = i;
			point2.second = i;

			while ((unsigned char) ppImageArrays[3][pCornerInfo->GetConverted(point1,3)] == scale)
			{
				if (holeheight && ((unsigned char) ppImageArrays[3][pCornerInfo->GetConverted(point2,3)] == scale))
					holeheight = false;
				
				point1.second--;
				point2.second--;
			}

			holewidth[1] = point1.second;

			if ((abs (holewidth[1] - holewidth[0]) > 5) && !holeheight)
				return false;

			diagonal = true;
			while(true)
			{
				diagonal = true;
				point3.first--;
				while ((unsigned char) ppImageArrays[3][pCornerInfo->GetConverted(point3,3)] == scale)
				{
					point3.first--;
					diagonal = false;
				}
				point3.first++;
				if(diagonal)
				{
					if ((((point3.first - 5) * (point3.first - 5)) + ((point3.second - i) * (point3.second - i))) > 255)
						return false;
					else
						break;
				}

				point3.second++;
				while ((unsigned char) ppImageArrays[3][pCornerInfo->GetConverted(point3,3)] == scale)
				{
					point3.second++;
					diagonal = true;
				}
				point3.second--;
				if (!diagonal)
				{
					point3.second--;
					while ((unsigned char) ppImageArrays[3][pCornerInfo->GetConverted(point3,3)] == scale)
					{
						point3.second--;
						diagonal = true;
					}
					point3.second++;
				}
			}
		}
	}
	return true;
}